package com.bp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author andhini
 */
public class Paket {

    public String id_paket;
    public String nama_paket;
//    public double harga;
    public String harga_paket;
    public String lama;
    public String lokasi;
    public String tujuan;
    public String fasilitas;
  

    public Paket() {
    }

    /**
     * @return the id_paket
     */
    public String getId_paket() {
        return id_paket;
    }

    /**
     * @param id_paket the id_paket to set
     */
    public void setId_paket(String id_paket) {
        this.id_paket = id_paket;
    }

    /**
     * @return the nama_paket
     */
    public String getNama_paket() {
        return nama_paket;
    }

    /**
     * @param nama_paket the nama_paket to set
     */
    public void setNama_paket(String nama_paket) {
        this.nama_paket = nama_paket;
    }

    /**
     * @return the harga
     */
//    public double getHarga() {
//        return harga;
//    }
//
//    /**
//     * @param harga the harga to set
//     */
//    public void setHarga(double harga) {
//        this.harga = harga;
//    }
    public String getharga_paket() {
        return harga_paket;
    }

    /**
     * @param harga the harga to set
     */
    public void setharga_paket(String harga_paket) {
        this.harga_paket = harga_paket;
    }

    /**
     * @return the lama_perjalanan
     */
    public String getLama() {
        return lama;
    }

    /**
     * @param lama_perjalanan the lama_perjalanan to set
     */
    public void setLama(String lama) {
        this.lama = lama;
    }

    /**
     * @return the lokasi
     */
    public String getLokasi() {
        return lokasi;
    }

    /**
     * @param lokasi the lokasi to set
     */
    public void setLokasi(String lokasi) {
        this.lokasi = lokasi;
    }

    /**
     * @return the tujuan
     */
    public String getTujuan() {
        return tujuan;
    }

    /**
     * @param tujuan the tujuan to set
     */
    public void setTujuan(String tujuan) {
        this.tujuan = tujuan;
    }

    /**
     * @return the fasilitas
     */
    public String getFasilitas() {
        return fasilitas;
    }

    /**
     * @param fasilitas the fasilitas to set
     */
    public void setFasilitas(String fasilitas) {
        this.fasilitas = fasilitas;
    }

    public void insertDataPaket() {
        try {
            Connection conn = new DbBean().getKoneksi();
            Statement s = conn.createStatement();
            String sql = "insert into PAKET values('" + id_paket + "', '" + nama_paket + "','" + harga_paket + "','" + lama + "','" + lokasi + "','" + tujuan + "','" + fasilitas + "')";
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Selamat data paket berhasil disimpan");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Maaf data paket gagal disimpan");
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final String getId_Paket() {
        try {
//            String sql = "select ID_PAKET from IDPAKET desc";
            String sql = "select ID_PAKET from ID_PAKET";
            Connection con = new DbBean().getKoneksi();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();


            String result = rs.getString(1);
            int no = Integer.parseInt(result);
            no++;
            sql = "" + no;
            while (sql.length() <= 3) {
                sql = "0" + sql;
            }

            sql = "update ID_PAKET set ID_PAKET  = '" + sql + "'";
            sta = con.createStatement();
            sta.executeUpdate(sql);
            con.close();

            return result;


        } catch (SQLException ex) {
            return "";
        }
    }
    public ArrayList lihatDataPaket() {
        ArrayList pakets = new ArrayList();
        try {
            Connection connection = new DbBean().getKoneksi();
            Statement s = connection.createStatement();
            String sql = "SELECT * from Paket order by ID_PAKET";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                Paket pkt = new Paket();
                pkt.id_paket = rs.getString(1);
                pkt.nama_paket = rs.getString(2);
                pkt.harga_paket = rs.getString(3);
                pkt.lama = rs.getString(4);
                pkt.lokasi = rs.getString(5);
                pkt.tujuan = rs.getString(6);
                pkt.fasilitas = rs.getString(7);
                pakets.add(pkt);
            }
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException e) {
        }
        return pakets;
    }
    
    public Boolean updatePaket(String id_Paket) {
        int result = 0;
        try {
            
            Connection conn = new DbBean().getKoneksi();
            Statement s = conn.createStatement();

            String sql = "UPDATE PAKET SET LAMA='" + lama + "',HARGA_PAKET" + harga_paket + ",LOKASI='" + lokasi + "'," +
                    "FASILITAS ='" + fasilitas + "',TUJUAN='" + tujuan + " WHERE ID_PAKET='" + id_Paket + "'";

            s.executeUpdate(sql);
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Data Paket berhasil diupdate");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            System.out.println("Data Paket Gagal Diupdate");
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }
    
        //method untuk menghapus data Paket
    public Boolean hapusPaket(String idPkt) {
        int result = 0;
        try {
            Connection conn = new DbBean().getKoneksi();
            Statement s = conn.createStatement();
            String sql = "DELETE from PAKET where ID_PAKET = '" + idPkt + "'";

            s.executeUpdate(sql);
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Data Paket berhasil dihapus");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            System.out.println("Data Paket Gagal Dihapus");
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }
}
